package dome_006;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Description: [对比上一个程序，可以用synchronized解决，
 * synchronize可以保证可见性和原子性，volatile只能保证可见性]</p>
 * Created on 2020/12/3
 *
 * @author <a href="mailto: 819712047@qq.com">谢振东 </a>
 * @version 1.0
 */
public class Dome006_02 {
    int count = 0;

    public synchronized void test() {
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

    public static void main(String[] args) {
        Dome006_02 dome006_02 = new Dome006_02();

        List<Thread> threads = new ArrayList<Thread>();

        for (int i = 0; i < 10; i++) {
            threads.add(new Thread(dome006_02::test, "thread-" + i));
        }

        threads.forEach((o) -> o.start()); //JDK1.8新特性

        threads.forEach((o) -> { //JDK1.8新特性
            try {
                o.join(); //等线程执行完毕之后才执行主线程main
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        System.out.println(dome006_02.count);
    }
}
